home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
Games-Dice.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
5KB
|
226 lines
" NAME Games-Dice
AUTHOR TPH@cs.man.ac.uk
FUNCTION alternative decision-making
ST-VERSIONS 2.2
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY Games-Dice
is a simple alternative to conventional decision-making!!
(2.2). TPH
"!
Model subclass: #Dice
instanceVariableNames: 'value randomGenerator '
classVariableNames: ''
poolDictionaries: ''
category: 'Games-Dice'!
Dice comment:
'I represent a single Dice (strictly, a DIE). I respond to the roll
message to generate a new random number.'!
!Dice methodsFor: 'initialize-release'!
initialize
"Initialize the random number generator, and set up the initial
state of the receiver."
randomGenerator _ Random new.
self rollDice! !
!Dice methodsFor: 'accessing'!
value
"Answer with the value represented by the receiver."
^value! !
!Dice methodsFor: 'modifying'!
roll
"Update the dice value randomly several times, to give the impression
of a real roll."
6 timesRepeat: [
self rollDice.
self changed]!
rollDice
"Update the dice value randomly."
value _ (randomGenerator next * 6 + 1) truncated! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
Dice class
instanceVariableNames: ''!
!Dice class methodsFor: 'instance creation'!
new
"Answer with a new instance of the receiver."
^super new initialize! !
MouseMenuController subclass: #DiceController
instanceVariableNames: ''
classVariableNames: 'DiceYellowButtonMenu DiceYellowButtonMessages '
poolDictionaries: ''
category: 'Games-Dice'!
DiceController comment:
'I represent the controller for a DiceView.'!
!DiceController methodsFor: 'initialize-release'!
initialize
"Initialize the yellow button menu."
super initialize.
self
yellowButtonMenu: DiceYellowButtonMenu
yellowButtonMessages: DiceYellowButtonMessages! !
!DiceController methodsFor: 'control defaults'!
isControlActive
^(view containsPoint: sensor cursorPoint) & sensor blueButtonPressed not! !
!DiceController methodsFor: 'menu messages'!
redButtonActivity
self model roll.
self sensor waitNoButton!
rollDice
self model roll! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
DiceController class
instanceVariableNames: ''!
!DiceController class methodsFor: 'class initialization'!
initialize
"DiceController initialize."
DiceYellowButtonMenu _ PopUpMenu
labels: 'roll' withCRs.
DiceYellowButtonMessages _ #(rollDice).! !
DiceController initialize!
View subclass: #DiceView
instanceVariableNames: 'cacheBox cachedForms '
classVariableNames: ''
poolDictionaries: ''
category: 'Games-Dice'!
DiceView comment:
'I know how to display a Dice in a viewable manner. I keep cached forms
for speed.'!
!DiceView methodsFor: 'initialize-release'!
initialize
"Initialize the array for cached forms."
cachedForms _ Array new: 6.
super initialize! !
!DiceView methodsFor: 'controller access'!
defaultControllerClass
^DiceController! !
!DiceView methodsFor: 'displaying'!
displayView
"Display the model. Re-create the cached forms if the display
box has changed."
(cacheBox isNil or: [cacheBox ~= self insetDisplayBox]) ifTrue: [
self makeForms.
cacheBox _ self insetDisplayBox copy].
(cachedForms at: self model value)
displayOn: Display
at: self insetDisplayBox topLeft!
update: aParameter
"Ignore aParameter, and recreate the display."
self display! !
!DiceView methodsFor: 'private'!
makeForms
"Construct the cached forms used for displaying."
| box center tempForm |
box _ self insetDisplayBox.
tempForm _ Form dotOfSize: (box extent x min: box extent y) // 5.
1 to: 6 do: [:each |
cachedForms at: each put: (Form extent: box extent).
box _ (cachedForms at: each) boundingBox.
center _ box center.
(#(1 3 5) includes: each) ifTrue: [
tempForm
displayOn: (cachedForms at: each)
at: center].
(#(2 3 4 5 6) includes: each) ifTrue: [
tempForm
displayOn: (cachedForms at: each)
at: center - ((center - box topLeft) // 2).
tempForm
displayOn: (cachedForms at: each)
at: center + ((center - box topLeft) // 2)].
(#(4 5 6) includes: each) ifTrue: [
tempForm
displayOn: (cachedForms at: each)
at: center - ((center - box topRight) // 2).
tempForm
displayOn: (cachedForms at: each)
at: center + ((center - box topRight) // 2)].
(each = 6) ifTrue: [
tempForm
displayOn: (cachedForms at: each)
at: center - ((center - box leftCenter) // 2).
tempForm
displayOn: (cachedForms at: each)
at: center + ((center - box leftCenter) // 2)]]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
DiceView class
instanceVariableNames: ''!
!DiceView class methodsFor: 'instance creation'!
open
"Create a new instance on the receiver with a new model"
"DiceView open."
self openOn: Dice new!
openOn: aDice
"Create a new instance on the receiver with the model aDice."
"DiceView openOn: Dice new."
| topView diceView |
topView _ StandardSystemView
model: nil
label: nil
minimumSize: 66 @ 66.
diceView _ self new model: aDice.
diceView insideColor: Form white.
diceView borderWidth: 1.
topView addSubView: diceView.
topView controller open! !